package com.siki.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.siki.dto.position.GetPositionListDTO;
import com.siki.dto.position.ReportPositionDTO;
import com.siki.mapper.*;
import com.siki.pojo.commonEntity.*;
import com.siki.service.UserTalentPositionService;
import com.siki.vo.position.TalentPositionUserListVO;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * @Author Siki
 * @Date 2020/6/16
 */
@Service
public class UserTalentPostionServiceImpl implements UserTalentPositionService {

    private final UserMapper userMapper;
    private final GeneralUserMapper generalUserMapper;
    private final TalentPositionMapper talentPositionMapper;
    private final GeneralPositionMapper generalPositionMapper;
    private final ApplyGeneralPositionMapper applyGeneralPositionMapper;

    public UserTalentPostionServiceImpl(UserMapper userMapper,
                                        GeneralUserMapper generalUserMapper,
                                        TalentPositionMapper talentPositionMapper,
                                        GeneralPositionMapper generalPositionMapper,
                                        ApplyGeneralPositionMapper applyGeneralPositionMapper) {
        this.userMapper = userMapper;
        this.generalUserMapper = generalUserMapper;
        this.talentPositionMapper = talentPositionMapper;
        this.generalPositionMapper = generalPositionMapper;
        this.applyGeneralPositionMapper = applyGeneralPositionMapper;
    }

    @Override
    public IPage<TalentPositionUserListVO> getPositionList(GetPositionListDTO dto) {
        QueryWrapper<TalentPosition> queryWrapper = new QueryWrapper<>();
        //有则添加条件,没有就略过
        Optional.ofNullable(dto.getEducationClaim()).ifPresent(education -> {
            queryWrapper.eq("education_claim",education);
        });
        Optional.ofNullable(dto.getPositionCategory()).ifPresent(positionCategory->{
            queryWrapper.eq("position_category",positionCategory);
        });
        //只查询已发布的
        queryWrapper.eq("released",true);
        Page<TalentPosition> positionPage =
                new Page<>(dto.getPageNum() == null ? 1 : dto.getPageNum(), dto.getPageSize() == null ? 10 : dto.getPageSize());
        return talentPositionMapper.selectPage(positionPage,queryWrapper).convert(TalentPositionUserListVO::new);
    }

    @Override
    public void saveApplyPosition(Long id, String userId) {
        TalentPosition talentPosition = queryTalentPositionById(id);
        GeneralUser generalUser = queryUserById(userId).getGeneralUser();
        ApplyGeneralPosition applyGeneralPosition = new ApplyGeneralPosition();
        applyGeneralPosition.setGeneralUser(generalUser);
        applyGeneralPosition.setTalentPosition(talentPosition);
        //持久化数据
        applyGeneralPositionMapper.insertApplyPosition(applyGeneralPosition);
    }

    @Override
    public void reportPosition(ReportPositionDTO dto, String userId) {
        TalentPosition talentPosition = queryTalentPositionById(dto.getId());
        GeneralUser user = queryUserById(userId).getGeneralUser();
        //关联
        GeneralUserTalentPosition generalUserTalentPosition = new GeneralUserTalentPosition();
        generalUserTalentPosition.setGeneralUser(user);
        generalUserTalentPosition.setTalentPosition(talentPosition);
        //持久化数据
        generalPositionMapper.insertReportPosition(generalUserTalentPosition);
    }

    private TalentPosition queryTalentPositionById(Long id){
        return Optional.ofNullable(talentPositionMapper.selectById(id))
                .orElseThrow(()->new RuntimeException("找不到岗位:"+id));
    }

    private SysUser queryUserById(String id){
        return Optional.ofNullable(userMapper.selectById(id))
                .orElseThrow(()->new RuntimeException("找不到用户:"+id));
    }
}
